;
(function (window, angular) {
    'use strict';
    angular.module("app.client", ["ui.router", "ngSanitize", "ui.bootstrap", "angular.filter", "YB.Widget", "toggle-switch", "toaster", "Directive.All", "Controller.All", "Service.All", "Filter.All", "template-directive", "template-widget"])
        .run(["$document", "$rootScope", "ybHttp", "ybConfig", "$state", function ($document, $rootScope, ybHttp, ybConfig, $state) {
            ybConfig.initConfig();
            $document.bind("selectstart dragstart contextmenu", function () {
                return false;
            });
            // $state.go("app.workBoard.main");
        }])

        .config(['$stateProvider', '$urlRouterProvider', "$logProvider", function ($stateProvider, $urlRouterProvider, $logProvider) {
            $logProvider.debugEnabled(true);

            $stateProvider.state("app", {
                url: "",
                abstract: true,
                templateUrl: "view/app.html"
            });

            $stateProvider.state("app.workBoard", {
                url: "/workBoard",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.merchant", {
                url: "/merchant",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.internal", {
                url: "/internal",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.system", {
                url: "/system",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.personal", {
                url: "/personal",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.basicDataManageCli", {
                url: "/basicDataManageCli",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.manageSaleManageCli", {
                url: "/manageSaleManageCli",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.managePurchaseManageCli", {
                url: "/managePurchaseManageCli",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.manageWarehouseManageCli", {
                url: "/manageWarehouseManageCli",
                abstract: true,
                template: "<div ui-view></div>"
            });

            $stateProvider.state("app.managerReport", {
                url: "/managerReport",
                abstract: true,
                template: "<div ui-view></div>"
            });

        }])

        .controller("AppCtrl", ['$rootScope', '$scope', "$state", "ybHttp", "UserContextService", "ybWidget", "EventBus", "$timeout", function ($rootScope, $scope, $state, ybHttp, UserContextService, ybWidget, EventBus, $timeout) {
            $rootScope.primaryMenuItem = {};
            $scope.app = {
                settings: {
                    sideBar: false
                }
            };
            $scope.state = $state;
            $scope.sysName = window.AppData.config.otherData.sysName;
            $scope.curUser = {};
            $scope.primaryMenus = [];
            $scope.subMenu = {};
            var goWorkBoard = true;
            var curMenu = "";
            UserContextService.GetAuthCodes();
            UserContextService.GetUserInfo();

            $scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
                curMenu = toState.name;
            });

            $scope.shadeInfo = {
                isShow: false,
                message: ""
            };
            $scope.$on("showLoading", function (event, msg) {
                $scope.shadeInfo.isShow = true;
                $scope.shadeInfo.message = msg || '加载中...';
            });
            $scope.$on("hideLoading", function (event) {
                $scope.shadeInfo.isShow = false;
            });

            $scope.goUrl = function (state) {
                $scope.subMenu.children = [];
                $scope.app.settings.sideBar = false;
                $state.go(state);
            };

            function getAuthMenu() {
                var service = "auth";
                var reqUrl = "/menu/authorized";
                ybHttp.Get(service, reqUrl).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp.message);
                        return;
                    }
                    $scope.primaryMenus = resp.value;

                    if ($scope.primaryMenus.length === 0) {
                        $state.go("app.workBoard.main");
                    } else {
                        $scope.navWidth = { width: $scope.primaryMenus.length * 94 + "px" };
                        var menuItem = resp.value[0];
                        if (!curMenu) {
                            if (!!goWorkBoard) {
                                $state.go("app.workBoard.main");
                            } else {
                                $scope.getSubMenu(menuItem);
                            }
                        } else {
                            $timeout(function () {
                                setSubMenu($scope.primaryMenus);
                            });
                        }
                    }
                });
            }

            function setSubMenu(menus) {
                var menu = "app." + curMenu.split(".")[1];
                var menuHasState = false;
                for (var i = 0, len = menus.length; i < len; i++) {
                    if (menus[i].state === menu) {
                        menuHasState = true;
                        $scope.app.settings.sideBar = true;
                        getSubMenu(menus[i]);
                        $rootScope.primaryMenuItem = menus[i];
                        EventBus.Publish("setCrumbsItem", menus[i]);
                        break;
                    }
                }
                if (!menuHasState) {
                    $scope.subMenu.children = [];
                }
            }

            function getSubMenu(menuItem) {
                if (menuItem.children.length > 0) {
                    $scope.subMenu = angular.copy(menuItem); 
                    goSubState(false);
                } else {
                    $state.go(menuItem.state);
                }
            }

            function goSubState(jumpToFirst) {
                if (!jumpToFirst) {
                    return;
                }
                if ($scope.subMenu.children[0].leaf === 1 && $scope.subMenu.children[0].state !== "") {
                    $state.go($scope.subMenu.children[0].state);
                }
                if ($scope.subMenu.children[0].leaf === 0) {
                    var thdItem = $scope.subMenu.children[0].children[0];
                    $state.go(thdItem.state);
                }
            }

            // 点击主导航触发
            $scope.getSubMenu = function (primaryMenu) {
                $scope.subMenu = angular.copy(primaryMenu);
                if ($scope.subMenu.children.length > 0) {
                    $scope.app.settings.sideBar = true;
                }
                $rootScope.primaryMenuItem = primaryMenu;
                goSubState(true);
            };

            $scope.getScreen = function () {
                console.log(window.location.href.split("#/"));
                var hrefList = window.location.href.split("main")
                var href = hrefList[0].concat("screen/index.html#/hanyun/index")
                console.log(href);
                window.open(href)
                
            };

            $scope.switchSubNav = function () {
                if (angular.isUndefined($scope.subMenu.children) || $scope.subMenu.children.length === 0) {
                    return;
                }
                $scope.app.settings.sideBar = !$scope.app.settings.sideBar;
            };

            $scope.clickMenu = function (subMenu) {
                if (subMenu.children.length > 0) {
                    subMenu.isCollapsed = !subMenu.isCollapsed;
                    return;
                }
                $state.go(subMenu.state)
            };

            $scope.logout = function () {
                UserContextService.Logout();
            };

            EventBus.Subscribe("getSubNav", getSubNav);

            function getSubNav(data) {
                var stateName = "app." + data.split(".")[1];
                for (var i = 0, len = $scope.primaryMenus.length; i < len; i++) {
                    if ($scope.primaryMenus[i].state === stateName) {
                        $scope.subMenu = angular.copy($scope.primaryMenus[i]);
                        break;
                    }
                }

                if ($scope.subMenu.children.length > 0) {
                    $scope.app.settings.sideBar = true;
                }
                $rootScope.primaryMenuItem = $scope.subMenu;
            }

            $scope.$on("$destroy", function () {
                EventBus.Unsubscribe("getSubNav", getSubNav);
            });

            EventBus.Subscribe("getUserInfo", getUserInfo);

            function getUserInfo(data) {
                $scope.curUser = data;
            }

            $scope.$on("$destroy", function () {
                EventBus.Unsubscribe("getUserInfo", getUserInfo);
            });

            (function () {
                getAuthMenu();
                // getMenuList();
            })();
        }])
        ;
})(window, window.angular);
